package com.company.huisuo.biz.activity

import android.app.Activity
import android.content.Intent
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.company.huisuo.base.BaseActivity
import com.company.huisuo.biz.dialog.MemberRechargeDialog
import com.company.huisuo.databinding.ActivityMemberBinding
import com.lxj.xpopup.XPopup
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 黑金会员
 */
class MemberActivity : BaseActivity<ActivityMemberBinding>() {

    private var mJob: Job? = null

    private val mRechargeDialog by lazy {
        XPopup.Builder(this).asCustom(MemberRechargeDialog(this) {
            mBinding.clContent.isVisible = true
        })
    }

    companion object {
        fun start(activity: Activity) {
            activity.startActivity(Intent(activity, MemberActivity::class.java))
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mJob?.cancel()
    }

    override fun initView() {
        mBinding.ivBack.setOnClickListener {
            finish()
        }
        mBinding.tvGetCode.setOnClickListener {
            countDown()
        }
        mBinding.tvNext.setOnClickListener {
            mRechargeDialog.show()
            mBinding.clContent.isVisible = false
        }
    }

    override fun initData() {

    }

    private fun countDown() {
        mJob = lifecycleScope.launch {
            mBinding.tvGetCode.isEnabled = false
            for (i in 60 downTo 1) {
                mBinding.tvGetCode.text = "${i}s"
                delay(1000)
            }
            mBinding.tvGetCode.text = "获取验证码"
            mBinding.tvGetCode.isEnabled = true
        }
    }
}